package task5_8;

import java.util.HashSet;
import java.util.Iterator;

public class HashSetTest {
    public static void main(String[] args) {
        //无序，唯一： HashSet
       HashSet<String> stringHashSet=new HashSet<String>();
        stringHashSet.add("张三");
        stringHashSet.add("里斯");
        stringHashSet.add("王五");
        stringHashSet.add("小三");
        stringHashSet.add("小三");

        /*for (String s:stringHashSet){
            System.out.println(s);
        }*/

        Iterator iterator=stringHashSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        /*错误，因为HashSet无序
        for(int i=0;i<stringHashSet.size();i++){
            System.out.println(stringHashSet.get(i));
        }*/


        HashSet<Student> studentHashSet=new HashSet<Student>();
        Student s1=new Student("rose",18);
        Student s2=new Student("rose",18);
        Student s3=new Student("jack",20);
        studentHashSet.add(s1);
        studentHashSet.add(s2);
        studentHashSet.add(s3);
        Iterator it=studentHashSet.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
}
